// pages/minePage/toOrderDetails/toOrderDetails.js
import {
  lgRequest
} from "../../../utils/ajax";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shangChuang: 'http://114.115.190.196:8082/profile/upload/2022/09/13/53ead76e72a504af6130b526634bf9a2.png',
    delectImgTag: "https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/07/19/713a49f43240fae4d44090d92d7f9cf6.png",
    workList: "",
    url: [],
    orderList: "",
    problemType: "",
    fileList: [],
    showFailureBypass: false,
    params: {},
    remark: '',
    showOrderFlow: false,
    overPeople: "",
    orderType: [{
        problemType: "集成",
        typeCode: 1
      },
      {
        problemType: "设备",
        typeCode: 2
      },
      {
        problemType: "网络",
        typeCode: 3
      },
      {
        problemType: "电力",
        typeCode: 4
      },
    ],
    isType: 0,
    problemType: [],
    showOrderFlowOne: false,
    isShetingShow: false,
    photerIcon: "https://www.mengbaoonline.com:8182/profile/upload/2022/11/26/3162b0c9d522bee0480915c4d3bcc1c3.png",
    switchListOne: [{
        name: "政府施工",
        name_active: "政府施工",
        isSelect: false,
        nameDetail: "政府施工",
        id: ''
      },
      {
        name: "不可抗力",
        name_active: "不可抗力",
        isSelect: false,
        nameDetail: "不可抗力",
        id: ''
      },
      {
        name: "其他",
        name_active: "其他",
        isSelect: false,
        nameDetail: "其他",
        id: ''
      },
    ],
    selectIndexType: 0,
    redBg: "https://www.mengbaoonline.com:8182/profile/upload/2022/11/24/9b362108a7b3d67529486e7bb8967388.png",
    greenBg: "https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/06/25/1f7f77a9b26ba601732b436a140c1e69.png",
    whiteBg: "https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/06/25/f37e1fe6501f44eb5fb877eff35068f2.png",
    circle: "https://www.mingchuliangzao.com.cn:8632/profile/upload/2022/06/25/ac0b6544db4ed907bf5444fc888409aa.png",

  },
  selectItemSwitchOne(e) {
    let index = e.currentTarget.dataset.index;
    let switchListOne = this.data.switchListOne;
    switchListOne[index].value = switchListOne[index].value == 1 ? "0" : "1";
    let selectSwitchItem = e.currentTarget.dataset.item;
    this.setData({
      switchListOne,
      selectSwitchItem,
    });
    console.log("状态按钮传参", e.currentTarget.dataset)
    let params = {
      id: this.data.switchListOne[index].id,
      value: this.data.switchListOne[index].value,
    };
    lgRequest("zhandiantingyon", params, {
      doSuccess: (r) => {
        console.log("修改成功", r)
      }
    })
  },
  // 流转说明
  liuzhuanshuoming(e) {
    // console.log(e.detail.value);
    this.setData({
      remark: e.detail.value
    })
  },
  // 站点停用确认
  ZhanTingQ(e) {
    let index = e.currentTarget.dataset.index;
    let switchListOne = this.data.switchListOne;
    // switchListOne[index].value = switchListOne[index].value == 1 ? "0" : "1";
    let selectSwitchItem = e.currentTarget.dataset.item;
    this.setData({
      switchListOne,
      selectSwitchItem,
    });
    let params = {
      // id: this.data.switchListOne.id,
      // value: [this.data.switchListOne[0].value, this.data.switchListOne[1].value, this.data.switchListOne[2].value],
      endTime: this.data.endTime,
      remark: this.data.remark,
      // content: [this.data.switchListOne[0].name, this.data.switchListOne[1].name, this.data.switchListOne[2].name],
      state: 6,
      siteId: this.data.undoneInfoList.zid,
      url: this.data.url
    };
    console.log(params, 'params');
    lgRequest("zhandiantingyon", params, {
      doSuccess: (r) => {
        // console.log("修改成功", r)
        this.setData({
          isShetingShow:false
        })
        wx.showToast({
          title: '修改成功',
        })
      }
    })
  },
  // 设备拆除
  Schaichu(){
    let params = {
      zId :this.data.undoneInfoList.zid,
      state :5
    }
    lgRequest("shebeichaichu", params, {
      doSuccess: (r) => {
        // console.log("修改成功", r)
        wx.showToast({
          title: '修改成功',
        })
      }
    })
  },
  CendTime(e) {
    // console.log(e.detail.value,'e');
    this.setData({
      endTime: e.detail.value
    })
  },
  //上传图片
  afterRead(event) {
    let that = this;
    // console.log(event)
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    let url = getApp().globalData.obLineSever + "server/picState";
    file.forEach((itemFile) => {

      wx.uploadFile({
        // url: "http://114.115.190.196:8082/common/upload",
        // url:"https://www.mingchuliangzao.com.cn:8632/common/upload",
        url: "https://www.mengbaoonline.com:8182/common/upload",
        filePath: itemFile.path,
        name: "file",
        formData: {
          user: "test"
        },
        header: {
          "Content-Type": "multipart/form-data",
          Authorization: getApp().globalData.Authorization,
        },
        success: (res) => {

          var urlArr = res.data.split(",")
          console.log(urlArr[3].split('"url":')[1], 'url');
          this.setData({
            url: urlArr[3].split('"url":')[1].substring(1,urlArr[3].split('"url":')[1].length-2)
          })
          // 上传完成需要更新 fileList 
          // let dataOne = JSON.parse(res.data);
          // console.log(res)

          wx.showToast({
            title: "上传成功！",
          });
          let params = {
            siteId: that.dataOne.zhanIdInfo.id,
            // siteId: this.data.undoneInfoList.zid,
            state: that.dataOne.selectIndexType + 1,
            pic: dataOne.url,
          }
          lgRequest("addPicState", params, {
            doSuccess: (res) => {
              // console.log(res)
              if (res.code == 200) {
                console.log(res, );
                this.picStateList()
              }
            }

          })


        },
        fail(error) {
          wx.showToast({
            title: "上传失败",
          });
          console.log(error, '这是上传图片的错误')
        },
      });

      // console.log(itemFile)

      // wx.uploadFile({
      //   url: "https://www.mingchuliangzao.com.cn:8632/common/upload",
      //   filePath: itemFile.path,
      //   name: "file",
      //   formData: {
      //     user: "test"
      //   },
      //   success(res) {
      //     // 上传完成需要更新 fileList
      //     let data = JSON.parse(res.data);
      //     let obj = {
      //       pic: data.url,
      //     };
      //     let picStateInfoList = that.data.picStateInfoList;
      //     picStateInfoList.unshift(obj);
      //     that.setData({
      //       picStateInfoList,
      //     });
      //   },
      //   fail(error) {
      //     wx.showToast({
      //       title: "上传失败，请稍后再试！",
      //     });
      //   },
      // });
    });
  },
  sheting() {
    let params = {
      siteId: this.data.undoneInfoList.zid
    }
    // switchListOne = this.data.switchListOne
    lgRequest('zhandiantingyonjilu', params, {
      doSuccess: r => {
        console.log(r, '站点停用变更记录')
        r.data.map((item) => {
          this.data.switchListOne.id = item.id
          // console.log(this.data.switchListOne.id,'this.data.switchListOne');
          return
        })
        this.setData({
          zhanTingList: r.data,
        })
      }
    })
    this.setData({
      isShetingShow: true,
    })
  },
  getZhanInfo() {
    let params = {
      zId: this.data.undoneInfoList.zid

    }
    lgRequest('siteList', params, {
      doSuccess: r => {
        this.setData({
          zhanInfo: r.data[0]
        })
      }
    })
  },
  // 关闭弹窗
  onClose() {
    let noProcessing = this.data.noProcessing;
    noProcessing.forEach(r => {
      r.is = false
    })
    let problemTag = this.data.problemTag;
    problemTag.forEach(r => {
      r.is = false
    })
    let params = {
      problemType: ''
    }
    this.setData({
      showFailureBypass: false,
      noProcessing,
      params,
      remark: "",
      showOrderFlow: false,
      showOrderCompletion: false,
      selectPeople: '',
      problemTag,
      problemType: '',
      showOrderFlowOne: false,
      showPic: false,
      isShetingShow: false

    })
  },
  // 跳转到工单详情页
  toStateDetail() {

    let zhanInfo = encodeURIComponent(JSON.stringify(this.data.zhanInfo));
    wx.navigateTo({
      url: `../stateDetail/stateDetail?zhanIdInfo=${zhanInfo}`,
    })
  },
  // 展示暂不处理
  showFailureBypass(e) {
    console.log(e)
    this.data.params.wid = e.currentTarget.dataset.item.wid;
    this.setData({
      showFailureBypass: true
    })

  },
  // 上传相册
  smbitPic(e) {
    let wid = e.currentTarget.dataset.item.wid;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      sourceType: ['album'],
      success: (res) => {
        console.log(res.tempFiles[0].tempFilePath)
        console.log(res.tempFiles[0].size)
        console.log('图片图片图片', res)
        res.tempFiles.forEach(item => {
          let file = item.tempFilePath

          wx.uploadFile({
            url: "https://www.mengbaoonline.com:8182/common/upload",
            // url: "http://114.115.190.196:8082/common/upload",
            filePath: file,
            name: "file",
            formData: {
              user: "test"
            },
            header: {
              Authorization: getApp().globalData.Authorization
            },
            success: (res) => {
              // 上传完成需要更新 fileList 
              let data = JSON.parse(res.data);
              console.log(data)
              wx.showToast({
                title: "上传成功！",
              });
              // if (res.code == 200) {
              let params = {
                wid,
                over: data.url,
              }
              lgRequest("smbitQuestionPic", params, {
                doSuccess: (res) => {
                  debugger
                  console.log('图片上传成功', res)
                  this.getOrderList()
                }
              })
              // }

            },
            fail(error) {
              wx.showToast({
                title: "上传失败，请稍后再试！",
              });
              console.log(error)
            },
          });
          // let params = {
          //   wid,
          //   over:item.tempFilePath
          // }
          // lgRequest("smbitQuestionPic",params,{
          //   doSuccess:(res)=>{
          //     console.log('图片上传成功',res)
          //     this.getOrderList()
          //   }
          // })
        })

      }
    })

  },
  // 拍照
  phonePic(e) {
    let wid = e.currentTarget.dataset.item.wid;
    wx.chooseMedia({
      count: 9,
      mediaType: ['image'],
      sourceType: ['camera'],
      success: (res) => {
        console.log(res.tempFiles[0].tempFilePath)
        console.log(res.tempFiles[0].size)
        wx.uploadFile({
          url: "https://www.mengbaoonline.com:8182/common/upload",
          // url: "http://114.115.190.196:8082/common/upload",
          filePath: res.tempFiles[0].tempFilePath,
          name: "file",
          formData: {
            user: "test"
          },
          header: {
            Authorization: getApp().globalData.Authorization
          },
          success: (res) => {
            // 上传完成需要更新 fileList 
            let data = JSON.parse(res.data);
            console.log(data)
            wx.showToast({
              title: "上传成功！",
            });
            // if (res.code == 200) {
            let params = {
              wid,
              over: data.url,
            }
            lgRequest("smbitQuestionPic", params, {
              doSuccess: (res) => {
                debugger
                console.log('图片上传成功', res)
                this.getOrderList()
              }
            })
            // }

          },
          fail(error) {
            wx.showToast({
              title: "上传失败，请稍后再试！",
            });
            console.log(error)
          },
        });


      }
    })

  },
  // 获取工单详情
  getWorkList() {
    let params = {
      zId: this.data.undoneInfoList.zid
    }
    lgRequest("getWorkList", params, {
      doSuccess: (r) => {
        console.log(r)
        this.setData({
          workList: r.data
        })
      }
    })
  },
  // 获取问题列表
  getOrderList() {
    let params = {
      zId: this.data.undoneInfoList.zid
    }
    lgRequest("dList", params, {
      doSuccess: (r) => {
        console.log(r)
        let orderList = r.data;

        // if(orderList.over){
        orderList.forEach(item => {
          item.content = item.content.replace("null", '')
          // let arr = [];
          item.pic = (item.over || '').split(',')
          // item.over = arr
          console.log('item.pic', item.pic)
        })
        // }

        this.setData({
          orderList: orderList
        })
      }
    })
  },
  //获取问题类型
  getProblem() {
    let serve = getApp().globalData.server;
    let Authorization = getApp().globalData.Authorization
    wx.request({
      url: `${serve}server/dictionaries/problem_type`,
      header: {
        Authorization
      },
      success: (r) => {
        console.log(r)
        this.setData({
          problemType: r.data.data
        })
      }
    })
  },
  // 获取暂不处理原因
  noProcessing() {
    let params = {
      code: "failure_bypass"
    }
    lgRequest("dictionaries", params, {
      doSuccess: r => {
        console.log(r)
        this.setData({
          noProcessing: r.data
        })
      }
    })
  },
  // 暂不处理原因
  selectFailureBypass(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    let params = this.data.params;
    params.failureBypass = item.valueId;
    params.state = 4;
    this.setData({
      params
    })


    console.log(e)
    let noProcessing = this.data.noProcessing;
    noProcessing[index].is = !noProcessing[index].is
    this.setData({
      noProcessing
    })
  },
  // 暂不处理说明
  getFailureBypassRemark(e) {
    console.log(e.detail.value)
    this.data.params.remark = e.detail.value
    console.log(this.data.params.remark)
  },
  submitFailureBypass() {
    let params = this.data.params
    lgRequest("smbitQuestionPic", params, {
      doSuccess: r => {
        console.log(r)
        this.getOrderList(); //获取工单列表
        this.onClose();

      }
    })
  },
  // 工单流转
  showOrderFlow(e) {
    this.data.params.wid = e.currentTarget.dataset.item.wid
    this.setData({
      showOrderFlow: true
    })
  },
  // 组内流转
  showOrderFlowOne() {
    // let principalOne = wx.getStorage({
    //   key:'userInfo',
    //   success:function(res){
    //     var principal = res.data.userGroup
    //     // return principal
    //     console.log(principal);

    //   }
    // })
    // console.log(principalOne);
    const Cates =wx.getStorageSync("userInfo");
    let principal = Cates.userId
    console.log(principal,'Cates');
    wx.request({
      url: `https://www.mengbaoonline.com:8182/server/workOther/userGroup/${principal}`,
      method: 'GET',
      dataType: 'json',
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token'),
        // 'headerUserId': user.id,
        // 'headerUserToken': user.userToken
      },
      success: (res) => {
        console.log(res.data.data,'res');
        this.setData({
          liuzhuanList:res.data.data
        })
      },
    });
    this.setData({
      showOrderFlowOne: true
    })
  },
  // 组内流转
  Goliuzhuan(e){
    // console.log(e.currentTarget.dataset.item.nickName);
    let params ={
      conductor :e.currentTarget.dataset.item.userId,
      wId :this.data.undoneInfoList.wid
    }
    console.log(params);
    lgRequest("zuneiliuzhuan", params, {
      doSuccess: res => {
        console.log(res);
        this.setData({
          showOrderFlowOne:false
        })
        wx.showToast({
          title: '修改成功!',
        })
      }
    })
  },
  // 得得得得得的额
  overPeople() {
    let params = {
      // code: "group"
      zhanId: this.data.undoneInfoList.zid

    }
    lgRequest("userList1", params, {
      doSuccess: r => {
        console.log(r)
        let overPeople = r.data
        overPeople.forEach(item => {
          item.value = item.userId;
          item.text = item.roleName;
        })
        this.setData({
          overPeople

        })
      }
    })
  },
  // 展示流转人
  showPeople() {
    this.setData({
      showPeople: true
    })
  },
  // 关闭流转人窗口
  onCloseC() {
    this.setData({
      showPeople: false
    })
  },
  // 选择流转人
  selectPeople(e) {
    console.log(e)
    let selectPeople = e.currentTarget.dataset.item.nickName;
    this.data.params.conductor = e.currentTarget.dataset.item.userId;
    this.setData({
      selectPeople
    })
    this.onCloseC();
  },
  // 选择问题
  selectType(e) {
    console.log(e)
    let isType = e.currentTarget.dataset.index;
    let typeCode = e.currentTarget.dataset.typeCode;

    this.setData({
      isType
    })

  },
  // 获取工单流转问题标签
  getProblemTag() {
    let params = {
      code: "problem_type",
    }
    lgRequest("dictionaries", params, {
      doSuccess: r => {
        console.log(r);
        this.setData({
          problemTag: r.data,
        })
      }
    })
  },
  // 选择工单流转问题
  selectproblemTag(e) {

    console.log(e)
    let item = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    let problemTag = this.data.problemTag;

    this.data.problemType.push(item.valueId)
    console.log(this.data.problemType)
    problemTag[index].is = !problemTag[index].is
    this.data.params.problemType = this.data.problemType.toString();

    this.setData({
      problemTag
    })
  },
  // 获取流转说明
  getOrderFlow(e) {
    console.log(e.detail.value)
    this.data.params.mvremark = e.detail.value
  },
  // 提交工单流转信息
  submitOrderFlow() {
    this.data.params.dqGps = this.data.workList.gps;
    let params = this.data.params;
    lgRequest("submitOrderFlow", params, {
      doSuccess: r => {
        console.log(r)
        this.getOrderList(); //获取工单列表
        this.onClose();
      }
    })

  },
  showOrderCompletion(e) {
    this.data.params.wid = e.currentTarget.dataset.item.wid
    this.setData({
      showOrderCompletion: true
    })
  },
  // 完成工单确定
  OrderCompletion() {
    this.data.params.zid = this.data.undoneInfoList.zid;
    this.data.params.dqGps = this.data.undoneInfoList.gps;
    this.data.params.conductor = this.data.undoneInfoList.conductor;
    this.data.params.state = 2;
    let params = this.data.params
    lgRequest("smbitQuestionPic", params, {
      doSuccess: r => {
        console.log(r)
        this.getOrderList(); //获取工单列表
        this.onClose();
      }
    })
  },
  // 转到历史工单

  toHistory() {
    // debugger
    let info = encodeURIComponent(JSON.stringify(this.data.undoneInfoList));
    wx.navigateTo({
      url: `../historyWorkList/historyWorkList?info=${info}`,
      fail: err => {
        console.log(err)
      }
    })
  },
  // 转到流转记录
  totransfer(e) {
    // let info = e.currentTarget.dataset.item
    let info = encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item));
    wx.navigateTo({
      url: `../transferRecord/transferRecord?info=${info}`,
      fail: err => {
        console.log(err)
      }
    })
  },
  showPic(e) {
    this.setData({
      showPic: true,
      itemPic: e.currentTarget.dataset.item,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options,'options')
    this.setData({
      undoneInfoList: JSON.parse(decodeURIComponent(options.info)),
      // undoneInfoListOne: JSON.parse(options.infos)
    })
    // console.log(this.data.undoneInfoListOne);
    this.getWorkList(); //获取工单详情
    this.getOrderList(); //获取工单列表
    this.noProcessing(); //获取暂不处理原因
    this.overPeople();
    this.getProblemTag(); //获取标签列表
    // this.getProblem();//获取标签列表
    this.getZhanInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})